from pylons import g
from pyleo.lib.mime_type import mime_type
from pyleo.model.Resource import resource
import urllib
import os

def lfs_content(lfs,path):
    ''' 
        Returns two lists of lfs content at a given path, 
            directories and files 
        each of which consists of dictionaries with the 
        url and title of the individual entities.
        
    '''
    pathLeof=resource(path)
    #c.listingOn=pathLeof.get_property('listingOn')
    #if not c.listingOn: return [],[]
    dirs,files=g.lfs.get_children(path.strip('/'))
    rdirs,rfiles=[],[]
    for d in dirs:
        x={}
        x['url']=urllib.quote(os.path.join(path,d))
        x['title']=d
        rdirs.append((x['title'],x))
    for f in files:
        x={}
        fullPath=os.path.join(path,f)
        x['url']=urllib.quote(fullPath)
        leof=g.lfs.get(fullPath)
        #print leof.get_properties()
        x['title']=(leof.get_property('page_title') or f)
        print x['title'],leof.get_content_type()
        x['mime_type']=mime_type(leof.get_content_type())
        rfiles.append((x['title'],x))
    rdirs.sort()
    rfiles.sort()    
    return [i[1] for i in rdirs],[i[1] for i in rfiles]